;(function() {

  /**
   * 账户状态
   */
  var accountReducer = pip.redux.createReducer(
    {
      role: pip.cookie.get('account_role') || 'guest',
      login: pip.cookie.get('account_role') ? true : false
    },
    {
      'account.login': function(state, action){
        state.role = action.role;
        state.login = true;
        pip.cookie.set('account_role', action.role);
        return state;
      },
      'account.logout': function(state){
        state.role = 'guest';
        state.login = false;
        pip.cookie.remove('account_role');
        return state;
      }
    }
  );
  var accountStore = pip.emitter(
    Redux.createStore(accountReducer)
  );


  /**
   * 用户信息
   */
  var userReducer = pip.redux.createReducer(
    {
      id: "",
      name: "",
      nickname: "",
      sex: "",
      avatar_path: "",
      birthday: ""
    },
    {

    }
  );


  /**
   * 合并 Reducer 生成 store
   */
  var store = Redux.createStore(
    Redux.combineReducers({
      account: accountReducer,
      user: userReducer
    })
  );

  /**
   * 包裹成监听模式
   */
  store = pip.emitter(store);

  // 监听所有事件，并分发
  store.on('*', function(type, data) {
    store.dispatch(pip.assign({ type: type }, data));
  });

  var mixin = {
    Store: store,
    State: store.getState()
  };

  // 当 state 发生变化就更新tag view（免去手动）
  store.subscribe(function(){
    riot.update(mixin.State);
  });

  riot.mixin('redux', mixin);

})();
